1 /* Copyright 2002-2016 CS Systèmes d'Information
2 * Licensed to CS Systèmes d'Information (CS) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * CS licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.orekit.forces.gravity.potential;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.text.ParseException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.Precision;
30 import org.orekit.errors.OrekitException;
31 import org.orekit.errors.OrekitMessages;
32 import org.orekit.utils.Constants;
33
34 /**This reader is adapted to the EGM Format.
35 *
36 * <p> The proper way to use this class is to call the {@link GravityFieldFactory}
37 * which will determine which reader to use with the selected gravity field file.</p>
38 *
39 * @see GravityFieldFactory
40 * @author Fabien Maussion
41 */
42 public class EGMFormatReader extends PotentialCoefficientsReader {
43
44 /** Flag for using WGS84 values for equatorial radius and central attraction coefficient. */
45 private final boolean useWgs84Coefficients;
46
47 /** Simple constructor.
48 * @param supportedNames regular expression for supported files names
49 * @param missingCoefficientsAllowed if true, allows missing coefficients in the input data
50 */
51 public EGMFormatReader(final String supportedNames, final boolean missingCoefficientsAllowed) {
52 this(supportedNames, missingCoefficientsAllowed, false);
53 }
54
55 /**
56 * Simple constructor that allows overriding 'standard' EGM96 ae and mu with
57 * WGS84 variants.
58 *
59 * @param supportedNames regular expression for supported files names
60 * @param missingCoefficientsAllowed if true, allows missing coefficients in the input data
61 * @param useWgs84Coefficients if true, the WGS84 values will be used for equatorial radius
62 * and central attraction coefficient
63 */
64 public EGMFormatReader(final String supportedNames, final boolean missingCoefficientsAllowed,
65 final boolean useWgs84Coefficients) {
66 super(supportedNames, missingCoefficientsAllowed);
67 this.useWgs84Coefficients = useWgs84Coefficients;
68 }
69
70
71 /** {@inheritDoc} */
72 public void loadData(final InputStream input, final String name)
73 throws IOException, ParseException, OrekitException {
74
75 // reset the indicator before loading any data
76 setReadComplete(false);
77
78 // both EGM96 and EGM2008 use the same values for ae and mu
79 // if a new EGM model changes them, we should have some selection logic
80 // based on file name (a better way would be to have the data in the
81 // file...)
82 if (this.useWgs84Coefficients) {
83 setAe(Constants.WGS84_EARTH_EQUATORIAL_RADIUS);
84 setMu(Constants.WGS84_EARTH_MU);
85 } else {
86 setAe(Constants.EGM96_EARTH_EQUATORIAL_RADIUS);
87 setMu(Constants.EGM96_EARTH_MU);
88 }
89
90 final String lowerCaseName = name.toLowerCase(Locale.US);
91 if (lowerCaseName.contains("2008") || lowerCaseName.contains("zerotide")) {
92 setTideSystem(TideSystem.ZERO_TIDE);
93 } else {
94 setTideSystem(TideSystem.TIDE_FREE);
95 }
96
97 final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
98 final List<List<Double>> c = new ArrayList<List<Double>>();
99 final List<List<Double>> s = new ArrayList<List<Double>>();
100 boolean okFields = true;
101 for (String line = r.readLine(); okFields && line != null; line = r.readLine()) {
102 if (line.length() >= 15) {
103
104 // get the fields defining the current the potential terms
105 final String[] tab = line.trim().split("\\s+");
106 if (tab.length != 6) {
107 okFields = false;
108 }
109
110 final int i = Integer.parseInt(tab[0]);
111 final int j = Integer.parseInt(tab[1]);
112 if (i <= getMaxParseDegree() && j <= getMaxParseOrder()) {
113 for (int k = 0; k <= i; ++k) {
114 extendListOfLists(c, k, FastMath.min(k, getMaxParseOrder()),
115 missingCoefficientsAllowed() ? 0.0 : Double.NaN);
116 extendListOfLists(s, k, FastMath.min(k, getMaxParseOrder()),
117 missingCoefficientsAllowed() ? 0.0 : Double.NaN);
118 }
119 parseCoefficient(tab[2], c, i, j, "C", name);
120 parseCoefficient(tab[3], s, i, j, "S", name);
121 }
122
123 }
124 }
125
126 if (missingCoefficientsAllowed() && getMaxParseDegree() > 0 && getMaxParseOrder() > 0) {
127 // ensure at least the (0, 0) element is properly set
128 extendListOfLists(c, 0, 0, 0.0);
129 extendListOfLists(s, 0, 0, 0.0);
130 if (Precision.equals(c.get(0).get(0), 0.0, 0)) {
131 c.get(0).set(0, 1.0);
132 }
133 }
134
135 if ((!okFields) || (c.size() < 1)) {
136 String loaderName = getClass().getName();
137 loaderName = loaderName.substring(loaderName.lastIndexOf('.') + 1);
138 throw new OrekitException(OrekitMessages.UNEXPECTED_FILE_FORMAT_ERROR_FOR_LOADER,
139 name, loaderName);
140 }
141
142 setRawCoefficients(true, toArray(c), toArray(s), name);
143 setReadComplete(true);
144
145 }
146
147 /** Get a provider for read spherical harmonics coefficients.
148 * <p>
149 * EGM fields don't include time-dependent parts, so this method returns
150 * directly a constant provider.
151 * </p>
152 * @param wantNormalized if true, the provider will provide normalized coefficients,
153 * otherwise it will provide un-normalized coefficients
154 * @param degree maximal degree
155 * @param order maximal order
156 * @return a new provider
157 * @exception OrekitException if the requested maximal degree or order exceeds the
158 * available degree or order or if no gravity field has read yet
159 * @since 6.0
160 */
161 public RawSphericalHarmonicsProvider getProvider(final boolean wantNormalized,
162 final int degree, final int order)
163 throws OrekitException {
164 return getConstantProvider(wantNormalized, degree, order);
165 }
166
167 }